package com.peanut.controller;

import cn.hutool.json.JSONUtil;
import com.peanut.common.Result;
import com.peanut.domain.PeanutFavourite;
import com.peanut.domain.PeanutFoot;
import com.peanut.domain.po.TabItem;
import com.peanut.dto.TabItemDTO;
import com.peanut.dto.TabUserDTO;
import com.peanut.service.TabItemService;
import com.peanut.util.IdWorker;
import com.peanut.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("mongo")
public class MongoController {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TabItemService tabItemService;

    @PostMapping("favourite")
    @ApiOperation("增加收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "密钥",required = true,paramType = "body"),
            @ApiImplicitParam(name = "peanutFavourite",value = "收藏实体",required = true,paramType = "body"),
    })
    public Result toAddFavourite(@RequestBody PeanutFavourite peanutFavourite,
                                 @PathVariable("id") Long id,
                                 @RequestHeader("AuthToken") String token){
        try {
            Claims claims = new JwtUtil().parseJwt(token);
            TabUserDTO userDTO = JSONUtil.toBean(claims.getSubject(), TabUserDTO.class);
            Long userId = userDTO.getUserId();
            if (userId == null){
                return Result.LOGIN_ERROR();
            }
            peanutFavourite.setId((Long) idWorker.nextId());
            mongoTemplate.insert(peanutFavourite);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @PostMapping("foot")
    @ApiOperation("增加足迹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "密钥",required = true,paramType = "body"),
            @ApiImplicitParam(name = "peanutFoot",value = "足迹实体",required = true,paramType = "body"),
    })
    public Result toAddFoot(@RequestBody PeanutFoot peanutFoot,
                            @PathVariable("id") Long id,
                            @RequestHeader("AuthToken") String token){
        try {
            Claims claims = new JwtUtil().parseJwt(token);
            TabUserDTO userDTO = JSONUtil.toBean(claims.getSubject(), TabUserDTO.class);
            Long userId = userDTO.getUserId();
            if (userId == null){
                return Result.LOGIN_ERROR();
            }
            peanutFoot.setId((Long) idWorker.nextId());
            mongoTemplate.insert(peanutFoot);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @PostMapping("selectFavById")
    @ApiOperation("通过用户id查询收藏表")
    @ApiImplicitParam(name = "token",value = "密钥",required = true,paramType = "body")
    public Result selectFavouriteByUserId(@PathVariable("id") Long id,@RequestHeader("AuthToken") String token){
        Claims claims = new JwtUtil().parseJwt(token);
        TabUserDTO userDTO = JSONUtil.toBean(claims.getSubject(), TabUserDTO.class);
        Long userId = userDTO.getUserId();

        if (userId == null){
            return Result.LOGIN_ERROR();
        }else {
            Query query = new Query(Criteria.where("user_id").regex(String.valueOf(userId)));
            List<PeanutFavourite> peanutFavourites = mongoTemplate.find(query, PeanutFavourite.class);

            List<Long> list = new ArrayList<>();
            for (PeanutFavourite peanutFavourite : peanutFavourites) {
                list.add(peanutFavourite.getCarId());
            }
            List<TabItem> list1 = new ArrayList<>();
            for (Long aLong : list) {
                TabItem tabItem = tabItemService.selectById(aLong);
                list1.add(tabItem);
            }
            return Result.OK().setData(list1);
        }
    }

    @PostMapping("selectFootById")
    @ApiOperation("通过用户id查询足迹")
    @ApiImplicitParam(name = "token",value = "密钥",required = true,paramType = "body")
    public Result selectFootById(@PathVariable("id") Long id,@RequestHeader("AuthToken") String token){
        Claims claims = new JwtUtil().parseJwt(token);
        TabUserDTO userDTO = JSONUtil.toBean(claims.getSubject(), TabUserDTO.class);
        Long userId = userDTO.getUserId();

        if (userId == null){
            return Result.LOGIN_ERROR();
        }
        Query query = new Query(Criteria.where("user_id").regex(String.valueOf(userId)));
        List<PeanutFoot> peanutFoot = mongoTemplate.find(query, PeanutFoot.class);

        List<Long> list = new ArrayList<>();
        for (PeanutFoot peanutFoot1 : peanutFoot) {
            list.add(peanutFoot1.getCarId());
        }
        List<TabItem> list1 = new ArrayList<>();
        for (Long aLong : list) {
            TabItem tabItem = tabItemService.selectById(aLong);
            list1.add(tabItem);
        }
        return Result.OK().setData(list1);
    }

    @PostMapping("selectDouble")
    @ApiOperation("查询对比")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "密钥",required = true,paramType = "body"),
            @ApiImplicitParam(name = "carId1",value = "第一个车辆id",required = true,paramType = "body"),
            @ApiImplicitParam(name = "carId2",value = "第二个车辆id",required = true,paramType = "body"),
    })
    public Result selectDouble(@PathVariable("id") Long id,@RequestHeader("AuthToken") String token,
                               @RequestParam Long carId1,
                               @RequestParam Long carId2){
        Claims claims = new JwtUtil().parseJwt(token);
        TabUserDTO userDTO = JSONUtil.toBean(claims.getSubject(), TabUserDTO.class);
        Long userId = userDTO.getUserId();
        if (userId == null){
            return Result.LOGIN_ERROR();
        }
        List<TabItem> list1 = new ArrayList<>();
        TabItem tabItem1 = tabItemService.selectById(carId1);
        TabItem tabItem2 = tabItemService.selectById(carId2);
        list1.add(tabItem1);
        list1.add(tabItem2);
        return Result.OK().setData(list1);
    }
}